import {post,userInfoAPI ,registerAPI, loginAPI, changePwdAPI,chargeRecordHistoryAPI,chargeWithdrawHistoryAPI,
  bindBankAPI,banklistAPI,logoutAPI,changeWithdrawPwdAPI,chargeAlllistAPI,
  changeUserInfoAPI,userbanklistAPI,userMessageAPI,userWithdrawAPI,getRecordsInfoAPI
,OrderlistAPI,fastPayList,bankPayList,saveIncBankPayAPI,BankTransferType,payTraditionBankAPI,allPayList,payOnlineQuickAPI,getTemplateAPI} from "../../utils/url";
import * as userType from "../types/user";
import {RES_OK_CODE} from "../types/index" 
import { log } from "util";

export function loadUserInfo(userName = '') {
  return (dispatch, getState) => {
    let state = getState();
    if (!userName) {
      userName = state.user.get('auth').get('userName');  
    }
    
    post(userInfoAPI(), {
      userName
    })
    .then(res => res.json())
    .then(data => {
      try {
        dispatch({
          type: userType.REQUEST_USER_INFO,
          data: data
        });
      } catch (e) {
        //console.error(e.toString());
      }
    });
  };
}

export function userRegister(params, cb = () => {}) {
  return (dispatch, getState) => {
    post(registerAPI(), params)
    .then(res => res.json())
    .then(data => {
      cb(data);
    });
  };
}
// 开发使用
export function userLogin(params,cb= ()=>{}) {
    return (dispatch, getState) =>{
      post(loginAPI(),params)
    //   fetch(loginAPI(),Object.assign({}, {
    //     credentials: 'same-origin'
    //   }, {
    //     method: 'POST',
    //     headers: {
    //       'Content-Type': 'application/x-www-form-urlencoded',
    //       "web_flag":"e0",
    //       "device_type":"wap",
    //       "platform_type":"wap",
    //       'device_id':'C01F45E97463E053222ABB584FE13859',
    //       'web_domain':'168ylc_domian',
    //       'client_ip':'199.22.431.14',
    //       'user_name':'user_name'
    //     },
    //     body: "userName=nihaoma&password=123qwe&accessToken=ae2f53a205e3514df09f441b4d30cd6d940ac0bab1ed162167ddb24a2f1cd0471d1323b347b28e6196ede2317dba8a891d0612f5154ac450387540a398eed5b74874d9293a33ccf8&clientKey=f90de75608641eec627bca292f7abf41&clientSecret=d29d5926683e28dd5cbeae736f217d00&state=111123"
    // })) // 开发阶段使用
      .then(res => res.json())
      .then(data =>{
        if (data.errorCode  == RES_OK_CODE) {
          let userName = data.datas.userName;
        //  let userName = data.datas.userInfo.userName // 开发阶段使用
          cb(true);
          dispatch({
            type: userType.REQUEST_USER_LOGIN,
            data: {
              userName
            },
          });
          dispatch(loadUserInfo(userName));
        } else {
          cb(false, data.msg);
        }
      })
    }
}

export function userLogout(cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(logoutAPI(), {
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        dispatch({
          type: userType.REQUEST_USER_LOGINOUT,
          data: {
            userName
          },
        })
        cb(true,json.datas)}
      else{
        cb(false,json.msg)
      }
    });
  };
}


export function changeLoginPwd(oldPwd, newPwd, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(changePwdAPI(), {
      userName,
      newPwd,
      oldPwd
    })
    .then(res => res.json())
    .then(json => {
      cb(json);
    });
  }
}

export function changeWithdrawPwd(oldPwd, newPwd, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(changeWithdrawPwdAPI(), {
      userName,
      newPwd,
      oldPwd
    })
    .then(res => res.json())
    .then(json => {
      cb(json);
    });
  }
}

export function changeUserBasicInfo(oldPwd, newPwd, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(changePwdAPI(), {
      userName,
      newPwd,
      oldPwd
    })
    .then(res => res.json())
    .then(json => {
      cb(json);
    });
  }
}


export function changeUserInfo(userMail,userBirthDay,userGender,userNickName,cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(changeUserInfoAPI(), {
      userName,
      userMail,
      userBirthDay,
      userGender,
      userNickName
    })
    .then(res => res.json())
    .then(json => {
      cb(json);
    });
  }
}



export function loadChargeRecord(beginTime, endTime, pageNo = 1, pageSize = 10,cb = () => {} ) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(chargeRecordHistoryAPI(), {
      userName,
      beginTime,
      endTime,
      pageNo,
      pageSize
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) { cb(true,json.datas)}
      else{
        cb(false,json.msg)
      }
     
    });
  };
}



export function loadChargeWithdraw(beginTime, endTime, pageNo = 1, pageSize = 10,cb = () => {} ) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(chargeWithdrawHistoryAPI(), {
      userName,
      beginTime,
      endTime,
      pageNo,
      pageSize
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) { cb(true,json.datas)}
      else{
        cb(false,json.msg)
      }
     
    });
  };
}

export function loadOrderlist( orderType, pageNo = 1, gameCode,beginTime,endTime,cb = () => {} ) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(OrderlistAPI(), {
      userName,orderType,
      pageNo,
      gameCode,
      beginTime,
      endTime,
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) { cb(true,json.datas)}
      else{
        cb(false,json.msg)
      }
     
    });
  };
}



export function getBankList(){
  return (dispatch ,getState) => {
    post(banklistAPI())
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: userType.REQUEST_BANK_LIST,
        data: data
      });
    })
  }
}

export function getuserBankList( cb=()=>{}){
  return (dispatch ,getState) => {
    let userName = getState().user.get('auth').get('userName');  
    post(userbanklistAPI(),{userName})
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: userType.REQUEST_USER_BANK_LIST,
        data: data
      });
      cb()
    })
  }
}



export function bindBank(bankType, bankCard, userRealName, bankAddress, withdrawPwd, cb = () => {}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(bindBankAPI(), {
      userName,
      bankType,
      bankCard,
      userRealName,
      bankAddress,
      withdrawPwd  
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) { cb(true,json.datas)}
      else{
        cb(false,json.msg)
      }
     
    });
  };
}


export function loadAllChargeList() {
  return (dispatch, getState) => {
    post(chargeAlllistAPI())
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: userType.REQUEST_CHANGE_BASIC_INFO,
        data
      });
    });
  };
}



export function loadUserMessage(readStatus,pageNo,pageSize) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(userMessageAPI(),{
      userName,readStatus,pageNo,pageSize
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: userType.REQUEST_USER_MESSAGE,
        data
      });
    });
  };
}

//充值

// 1.获取快捷支付列表
export function loadFastPayList() {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(fastPayList(),{
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        dispatch({
          type:userType.REQUEST_FAST_PAY_LIST,
          data:json.datas
        })
      }
      else{
        alert("获取快捷支付列表失败")
      }
    });
  };
}

// 2.获取公司列表
export function loadBankPayList() {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(bankPayList(),{
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        dispatch({
          type:userType.REQUEST_BANK_PAY_LIST,
          data:json.datas
        })
      }
      else{
        alert("获取公司列表失败")
      }
    });
  };
}

// 2.获取公司汇款方式列表
export function loadBankTransferType() {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(BankTransferType(),{
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        dispatch({
          type:userType.REQUEST_BANK_TRANSFER_TYPE,
          data:json.datas
        })
      }
      else{
        alert("获取公司汇款方式失败")
      }
    });
  };
}
// allPayList获取所有充值方式
export function loadAllPayList(params) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(allPayList(),{
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        dispatch({
          type:userType.REQUEST_BANK_ALL_LIST,
          data:json.datas
        })
      }
      else{
        alert("获取汇款方式失败")
      }
    });
  };
}

//在线支付 快捷支付表单 提交
export function payOnlineQuickWX(money,bankCode,payId,payValue,payType,cb=(ok,data)=>{}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(payOnlineQuickAPI(),{
      userName,
      money,
      bankCode,
      payId,payValue,payType
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        cb(true,json)
      }
      else{
        cb(false,json)
      }
    });
  };
}

export function payOnlineQuickAP(money,bankCode,payId,payValue,payType,pdFrpId,pa7CardAmt,pa8CardNo,pa9CardPwd,cb=(ok,data)=>{}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(payOnlineQuickAPI(),{
      userName,
      money,
      bankCode,
      payId,payValue,payType,pdFrpId,pa7CardAmt,pa8CardNo,pa9CardPwd
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        cb(true,json)
      }
      else{
        cb(false,json)
      }
    });
  };
}


//公司入款表单 提交
export function payTraditionBank(money,payNo,time,payType,companyBank,hkType,hkUserName,cb=(ok,data)=>{}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(payTraditionBankAPI(),{
      userName,
      money,
      payNo,
      time,payType,companyBank,hkType,hkUserName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        cb(true,json)
      }
      else{
        cb(false,json)
      }
    });
  };
}

export function saveIncBankPay(money,pageNo,id,time,companyBank,type,hkUserName,cb=(ok,data)=>{}) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(saveIncBankPayAPI(),{
      userName
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) {
        cb(true,json)
      }
      else{
        cb(false,json)
      }
    });
  };
}

//提现
export function userWithdraw(balance,withdrawPwd,bankId,cb) {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(userWithdrawAPI(),{
      userName,
      balance,
      withdrawPwd,
      bankId
    })
    .then(res => res.json())
    .then(json => {
      if (json.errorCode  == RES_OK_CODE) { cb(true,json.msg)}
      else{
        cb(false,json.msg)
      }
    });
  };
}


//获取今日盈亏
export function getRecordsInfo() {
  return (dispatch, getState) => {
    let userName = getState().user.get('auth').get('userName');
    post(getRecordsInfoAPI(),{
      userName
    })
    .then(res => res.json())
    .then(data => {
      dispatch({
        type: userType.REQUEST_TODAY_RECORDS,
        data
      })
    });
  };
}

// 获取模板接口
export function getTemplate(pageCode) {
  return (dispatch, getState) => {
    post(getTemplateAPI(),{
      pageCode
    })
    .then(res => res.json())
    .then(data => {
      console.log(pageCode.toUpperCase());
      
      dispatch({
        type: userType['REQUEST_TEMPLATE_'+pageCode.toUpperCase()],
        data
      })
    });
  };
}
